﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using Model;

namespace FG_Admin_DAL
{
    public class TicketsDAL
    {
        FGSkiingEntities model = new FGSkiingEntities();
        /// <summary>
        /// 分页显示所有雪场票价
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public List<Tickets> GetTickets(int PageIndex,int PageSize)
        {
            return model.Tickets.OrderBy(p=>p.Tickets_ID).Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
        }
        /// <summary>
        /// 显示当前雪场信息
        /// </summary>
        /// <param name="TicketsID">当前雪场票价编号</param>
        /// <returns></returns>
        public Tickets GetTickets(int TicketsID)
        {
            return model.Tickets.Where(p => p.Tickets_ID == TicketsID).FirstOrDefault();
        }
        /// <summary>
        /// 修改当前雪场信息
        /// </summary>
        /// <param name="tickets"></param>
        /// <returns></returns>
        public int EditTickets(Tickets tickets)
        {
            model.Entry(tickets).State = System.Data.EntityState.Modified;
            return model.SaveChanges();
        }
        /// <summary>
        /// 分页显示用户订单信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="UsersID">用户编号</param>
        /// <returns></returns>
        public List<FG_Admin_DAL.Models.TicketsMessage> GetTicketsBuy(int PageIndex,int PageSize,int UsersID)
        {
            #region
            List<Tickets_Buy> t_b = model.Tickets_Buy.Where(p=>p.Users_ID==UsersID).ToList();
            List<Tickets> t = model.Tickets.ToList();
            var list = from a in t_b
                       join b in t
                       on a.Tickets_ID equals b.Tickets_ID
                       select new
                       {
                           Tickets_ID = b.Tickets_ID,
                           Tickets_Name = b.Tickets_Name,
                           Tickets_Price = b.Tickets_Price,
                           Tickets_NewPrice = b.Tickets_NewPrice,
                           SnowPack_ID = b.SnowPack_ID,
                           Tic_ID = a.Tickets_Buy_ID,
                           Users_ID = a.Users_ID,
                           Count = a.Tickets_Buy_Count,
                           Price = a.Tickets_Buy_Price,
                           Status = a.Tickets_Buy_Status,
                           Start = a.Tickets_Buy_Start,
                           Expiry_Date = a.Expiry_Date,
                           Date = a.Tickets_Buy_Date,
                       };
            List<FG_Admin_DAL.Models.TicketsMessage> li = new List<Models.TicketsMessage>();
            foreach (var i in list)
            {
                FG_Admin_DAL.Models.TicketsMessage mes = new Models.TicketsMessage();
                mes.Tickets_ID = i.Tickets_ID;
                mes.Tickets_Name = i.Tickets_Name;
                mes.Tickets_Price = i.Tickets_Price;
                mes.Tickets_NewPrice = i.Tickets_NewPrice;
                mes.SnowPack_ID = i.SnowPack_ID;
                mes.Tic_ID = i.Tic_ID;
                mes.Users_ID = i.Users_ID;
                mes.Count = i.Count;
                mes.Price = i.Price;
                mes.Status = i.Status;
                mes.Start = i.Start;
                mes.Expiry_Date = i.Expiry_Date;
                mes.Date = i.Date;
                li.Add(mes);
            }
            #endregion
            return li;
        }
        /// <summary>
        /// 修改订单信息
        /// </summary>
        /// <param name="T_B"></param>
        /// <returns></returns>
        public int EditTicketsBuy(Tickets_Buy T_B)
        {
            model.Entry(T_B).State = System.Data.EntityState.Modified;
            return model.SaveChanges();
        }
        /// <summary>
        /// 分页显示用户退货信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="UsersID">用户编号</param>
        /// <returns></returns>
        public List<FG_Admin_DAL.Models.TicketsMessage> GetTicketsRefund(int PageIndex, int PageSize, int UsersID)
        {
            #region
            List<Tickets_Refund> t_b = model.Tickets_Refund.Where(p => p.Users_ID == UsersID).ToList();
            List<Tickets> t = model.Tickets.ToList();
            var list = from a in t_b
                       join b in t
                       on a.Tickets_ID equals b.Tickets_ID
                       select new
                       {
                           Tickets_ID = b.Tickets_ID,
                           Tickets_Name = b.Tickets_Name,
                           Tickets_Price = b.Tickets_Price,
                           Tickets_NewPrice = b.Tickets_NewPrice,
                           SnowPack_ID = b.SnowPack_ID,
                           Tic_ID = a.Tickets_Refund_ID,
                           Users_ID = a.Users_ID,
                           Count = a.Tickets_Refund_Count,
                           Price = a.Tickets_Refund_Price,
                           Status = a.Tickets_Refund_Status
                       };
            List<FG_Admin_DAL.Models.TicketsMessage> li = new List<Models.TicketsMessage>();
            foreach (var i in list)
            {
                FG_Admin_DAL.Models.TicketsMessage mes = new Models.TicketsMessage();
                mes.Tickets_ID = i.Tickets_ID;
                mes.Tickets_Name = i.Tickets_Name;
                mes.Tickets_Price = i.Tickets_Price;
                mes.Tickets_NewPrice = i.Tickets_NewPrice;
                mes.SnowPack_ID = i.SnowPack_ID;
                mes.Tic_ID = i.Tic_ID;
                mes.Users_ID = i.Users_ID;
                mes.Count = i.Count;
                mes.Price = i.Price;
                mes.Status = i.Status;
                li.Add(mes);
            }
            #endregion
            return li;
        }
        /// <summary>
        /// 修改退票信息
        /// </summary>
        /// <param name="T_R"></param>
        /// <returns></returns>
        public int EditTicketsRefund(Tickets_Refund T_R)
        {
            model.Entry(T_R).State = System.Data.EntityState.Modified;
            return model.SaveChanges();
        }

    }
}
